$(function() {
	var show = 0
	var navulwid = 0;
	console.log(121)
	console.log($('#scrollmain').width())
	for(var wid = 0; wid < $(".amnav>ul li").length; wid++) {
		navulwid += $(".amnav>ul li").eq(wid).width()
	}
	console.log($('#scrollmain').width() < navulwid)
	if($('#scrollmain').width() < navulwid) {

		$(".amnav>ul").width(navulwid+100);
		var orderScrollnav = new IScroll('.amnav', {
			mouseWheel: true,
			scrollX: true,
			click: true
		});
	}
	if($('#scrollmain').width() < $('.am-tabs-bd table').width()) {
		$(".am-tabs-bd>div").width($(".am-tabs-bd>div table").width());
		var orderScrollmain = new IScroll('.am-tabs-bd', {
			mouseWheel: true,
			scrollX: true,
			click: true,
			scrollbars: true

		});
	}
	$('.car_use').on('click', function() {
		choosecar("{:url('used/index',['op'=>'search'])}")
	});
	//	$('.orderertype').on('change', function() {
	//		if($('.orderertype').val() == 1) {
	//			console.log(1)
	//			$('.passenger').attr('name', 'passenger')
	//			$('.passenger').attr('type', 'text')
	//			$('.orderer').removeAttr('name')
	//			$('.orderer').attr('type', 'hidden')
	//		} else {
	//			console.log(2)
	//			$('.passenger').attr('type', 'hidden')
	//			$('.passenger').removeAttr('name')
	//			$('.orderer').attr('name', 'orderer')
	//			$('.orderer').attr('type', 'text')
	//
	//		}
	//	})
	$("#more").on("click", function() {
		if(show == 0) {

			$('#showmore').css('display', 'block')
			//			$('.arrive_time').attr('name', 'arrive_time')
			//			$('.complete_time').attr('name', 'complete_time')
			//			$('.apply_time').attr('name', 'apply_time')
			//			$('.approval_time').attr('name', 'approval_time')
			show = 1
		} else {
			$('#showmore').css('display', 'none')
			show = 0
			//			$('.arrive_time').removeAttr('name')
			//			$('.complete_time').removeAttr('name')
			//			$('.apply_time').removeAttr('name')
			//			$('.approval_time').removeAttr('name')
		}
	})
	$(".showmap").on('click', function() {
		popup(this)
		var that = this
		if($(this).data('method') == "showmap") {
			var trackload = layer.load(1, {
				shade: [0.1, '#fff'] //0.1透明度的白色背景
			});
			var map = new AMap.Map('showlocation', {
				resizeEnable: true,
				zoom: 8,
				center: [118.57, 24.82]
			});
			AMapUI.load(['ui/misc/PathSimplifier'], function(PathSimplifier) {
				var pathSimplifierIns = new PathSimplifier({
					zIndex: 100,
					map: map, //所属的地图实例
					getPath: function(pathData, pathIndex) {
						//返回轨迹数据中的节点坐标信息，[AMap.LngLat, AMap.LngLat...] 或者 [[lng|number,lat|number],...]
						return pathData.path;
					},
					getHoverTitle: function(pathData, pathIndex, pointIndex) {
						//返回鼠标悬停时显示的信息
						if(pointIndex >= 0) {
							//鼠标悬停在某个轨迹节点上
							return pathData.name + '，点:' + pointIndex + '/' + pathData.path.length;
						}
						//鼠标悬停在节点之间的连线上
						return pathData.name + '，点数量' + pathData.path.length;
					},
					renderOptions: {
						//轨迹线的样式
						pathLineStyle: {
							strokeStyle: 'red',
							lineWidth: 6,
							dirArrowStyle: true
						}
					}
				});
				$.ajax({
					type: "post",
					url: "{:url('getTrack')}",
					data: {
						oid: $(that).closest('tr').data('id')
					},
					success: function(res) {
						layer.close(trackload);
						var allpath = []
						var speed=0;
						if(typeof res == 'string') {
							var opath1 = JSON.parse(res).data;
						} else {
							var opath1 = res.data;
						}
						var opath2=[];
						var opath=[];
						//根据时间进行排序
						if(opath1.length>1){
							for(var rankidx=0;rankidx<opath1.length;rankidx++){
								opath2.push(opath1[rankidx].time)
							}
							opath2.sort()
							for(var k=0;k<opath2.length;k++){
								for(var j=0;j<opath1.length;j++){
									if(opath2[k]==opath1[j].time){
										opath.push(opath1[j])
									}
								}
							}
						console.log(opath)
						
						for(var oi = 0; oi < opath.length; oi++) {
							var oarr = []
							oarr.push(opath[oi].longitude)
							oarr.push(opath[oi].latitude)
							allpath.push(oarr)
						}
						initPage(PathSimplifier, pathSimplifierIns, map, allpath)
						}else{
							Err('暂无轨迹信息')
						}
					},
					error:function(){
						layer.close(carload);
						Err('请求失败')
					}
				});
			})
		}
	})
	$('.cancel').on('click', function() {
		popup(this, "{:url('state')}");
	});
	$('.consent').on('click', function() {
		popup(this, "{:url('state')}");
	});
	$('.repulse').on('click', function() {
		popup(this, "{:url('state')}");
	});
	$('.am-nav-tabs li a').on('click', function() {
		var index = layer.load(1, {
			shade: [0.1, '#fff'] //0.1透明度的白色背景
		});
	});

	function initPage(PathSimplifier, pathSimplifierIns, map, path) {
		var alljourney=0
		for(var pidx=0;pidx<path.length-1;pidx++){
			var  journey=Math.sqrt(Math.pow((path[pidx][0]-path[pidx+1][0]),2)+Math.pow((path[pidx][1]-path[pidx+1][1]),2))*111000
			alljourney+=journey
		}
		
		console.log(alljourney)
		//这里构建两条简单的轨迹，仅作示例
		pathSimplifierIns.setData([{
			name: '轨迹',
			path: path
		}]);
		
		//创建一个巡航器
		var navg0 = pathSimplifierIns.createPathNavigator(0, //关联第1条轨迹
			{
				loop: true, //循环播放
				speed:alljourney/10,
				pathNavigatorStyle: {
					width: 16,
					height: 32,
					content: PathSimplifier.Render.Canvas.getImageContent('{$static_dir}/assets/img/car.png', onload, onerror),
					strokeStyle: null,
					fillStyle: null
				}
			});

		navg0.start();
	}

})